package com.cmict.data.system.controller.admin.oa;

import cn.hutool.core.collection.CollUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.security.core.util.SecurityFrameworkUtils;
import com.cmict.data.system.controller.admin.oa.vo.*;
import com.cmict.data.system.dal.dataobject.oa.ScheduleManagementDO;
import com.cmict.data.system.service.oa.ScheduleManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.text.SimpleDateFormat;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "OA管理 - 日程管理")
@RestController
@RequestMapping("/system/schedule")
@Validated
public class ScheduleManagementController {

    @Resource
    private ScheduleManagementService scheduleManagementService;

    @PostMapping("/create")
    @ApiOperation("新增日程")
    @PreAuthorize("@ss.hasPermission('oa:schedule:create')")
    public CommonResult<Long> createScheduleManagement(@Valid @RequestBody ScheduleManagementCreateReqVO reqVO) {
        Long id = scheduleManagementService.createScheduleManagement(reqVO);
        return success(id);
    }

    @PutMapping("/update")
    @ApiOperation("修改日程")
    @PreAuthorize("@ss.hasPermission('oa:schedule:update')")
    public CommonResult<Boolean> updateScheduleManagement(@Valid @RequestBody ScheduleManagementUpdateReqVO reqVO) {
        scheduleManagementService.updateScheduleManagement(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除日程")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('oa:schedule:delete')")
    public CommonResult<Boolean> deleteScheduleManagement(@RequestParam("id") Long id) {
        scheduleManagementService.deleteScheduleManagement(id);
        return success(true);
    }

    @PostMapping("/calendar")
    @ApiOperation("日历页面获取日程数据")
    @PreAuthorize("@ss.hasPermission('oa:schedule:query')")
    public CommonResult<List<ScheduleManagementDO>> getScheduleManagementCalendar(@Valid @RequestBody ScheduleManagementCalendarReqVO reqVO) {
        reqVO.setCreator(SecurityFrameworkUtils.getLoginUserId());
        List<ScheduleManagementDO> list = scheduleManagementService.getScheduleManagementList(reqVO);
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得日程分页列表")
    @PreAuthorize("@ss.hasPermission('oa:schedule:query')")
    public CommonResult<PageResult<ScheduleManagementDO>> getScheduleManagementPage(@Valid ScheduleManagementPageReqVO reqVO) {
        reqVO.setCreator(SecurityFrameworkUtils.getLoginUserId());
        PageResult<ScheduleManagementDO> pageResult = scheduleManagementService.getScheduleManagementPage(reqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal())); // 返回空
        }
        return success(pageResult);
    }

//    @GetMapping("/nopush")
//    @ApiOperation("登录后获取未推送日程")
//    //@PreAuthorize("@ss.hasPermission('oa:schedule:list')")
//    public CommonResult<List<ScheduleManagementDO>> getScheduleManagementNoPush() {
//        ScheduleManagementNoPushReqVO reqVO = new ScheduleManagementNoPushReqVO();
//        reqVO.setCreator(SecurityFrameworkUtils.getLoginUserId());
//        reqVO.setSendTime(new Date());
//        Set<Integer> statusSet = new HashSet<>();
//        statusSet.add(0);
//        statusSet.add(1);
//        reqVO.setStatus(statusSet);
//        List<ScheduleManagementDO> list = scheduleManagementService.selectListNoPush(reqVO);
//        return success(list);
//    }

    @GetMapping("/laterremind")
    @ApiOperation("稍后提醒")
    @PreAuthorize("@ss.hasPermission('oa:schedule:update')")
    public CommonResult<Boolean> updateScheduleManagement(@RequestParam("id") Long id) {
        scheduleManagementService.updateLaterRemind(id);
        return success(true);
    }
}
